package com.wenhao.newcode.shopee;

/**
 * @author Wenhao Tong
 * @create 2021-07-17 13:50
 */
public class Main5 {
    public static void main(String[] args) {
        Main5 main5 = new Main5();
        boolean b = main5.checkValidString("(*))");
        System.out.println(b);

    }
    /**
     *
     * @param s string字符串
     * @return bool布尔型
     */
    public boolean checkValidString (String s) {
        int count = 0;
        int buffer = 0;
        for (int i = 0;i < s.length();i++) {
            char c = s.charAt(i);
            if (c == '(') {
                count++;
            }
            if (c == ')') {
                if (count > 0) {
                    count--;
                    continue;
                }
                if (buffer == 0) {
                    return false;
                }
                buffer--;
            }
            if (c == '*') {
                buffer++;
            }
        }
        count = 0;
        buffer = 0;
        for (int i = s.length() - 1;i > -1;i--) {
            char c = s.charAt(i);
            if (c == ')') {
                count++;
            }
            if (c == '(') {
                if (count > 0) {
                    count--;
                    continue;
                }
                if (buffer == 0) {
                    return false;
                }
                buffer--;
            }
            if (c == '*') {
                buffer++;
            }
        }
        return true;
    }
}
